<?php 
/**
#	Project: PHPDISK File Storage Solution
#	This is NOT a freeware, use is subject to license terms.
#
#	Site: http://www.phpdisk.com
#
#	$Id: ajax.php 4 2013-01-09 13:32:24Z along $
#
#	Copyright (C) 2008-2013 PHPDisk Team. All Rights Reserved.
#
*/

include "includes/commons.inc.php";

switch($action){
	case 'addtag':
	    $ptag_id = (int)gpc('ptag_id','G',0);
	    $tag_name = gpc('tag_name','G',0);
	    $tag_value = gpc('tag_value','G',0);
	    $tag_level = 1;
	    if($ptag_id && $ptag_id>0){
	    	$tag_level = 2;
	    }
	    if(checklength($tag_name,1,50)){
			echo '{"code":"0","msg":"tag_name_error"}';
			return;
		}
		if(checklength($tag_value,0,500)){
			echo '{"code":"0","msg":"tag_value_error"}';
			return;
		}
	    
	    $ins = array(
			'ptag_id' => $ptag_id,
			'level' => $tag_level,
			'tag_name' => $tag_name,
			'tag_value' => $tag_value);
	    
	    $rs = $db->fetch_one_array("select count(*) as total_num from {$tpf}nova_tags where tag_name='$tag_name'");
		$total_num = $rs['total_num'];
	    if($total_num>0){
	    	echo '{"code":"0","msg":"tag_exists"}';
			return;
	    }
	    
	    $db->query("insert into {$tpf}nova_tags set ".$db->sql_array($ins).";");

		echo '{"code":"1","msg":"save_tag_ok"}';
		break;
	case 'taglist':
	    $level = gpc('level','G',0);
	    $ptag_id = gpc('ptag_id','G',0);
	    
	    $q = $db->query("select * from {$tpf}nova_tags where level=$level and ptag_id=$ptag_id;");
		$tags = array();
		while ( $tag = $db->fetch_array($q) ) {
			$tags[]=$tag;
		};
		$db->free($q);
		echo '{"code":"1","msg":"save_tag_ok","data":'.json_encode($tags).'}';
		break;
	case 'updtag':
		$tag_id = (int)gpc('tag_id','G',0);
	    $tag_name = gpc('tag_name','G',0);
	    $tag_value = gpc('tag_value','G',0);
	    if($tag_name && checklength($tag_name,1,50)){
			echo '{"code":"0","msg":"tag_name_error"}';
			return;
		}
		if($tag_value && checklength($tag_value,0,500)){
			echo '{"code":"0","msg":"tag_value_error"}';
			return;
		}
	    
	    $upds = array();
	    if($tag_name){
	    	$upds['tag_name']=$tag_name;
	    }
	    if($tag_value){
	    	$upds['tag_value']=$tag_value;
	    }
	    echo count($upds);
	    echo $tag_name;
	    if(count($upds)<=0){
	    	echo '{"code":"0","msg":"no_info_update"}';
			return;
	    }
	    
	    $rs = $db->fetch_one_array("select count(*) as total_num from {$tpf}nova_tags where tag_id=$tag_id;");
		$total_num = $rs['total_num'];
	    if($total_num<=0){
	    	echo '{"code":"0","msg":"tag_not_exists"}';
			return;
	    }
	    
	    $rs = $db->fetch_one_array("select count(*) as total_num from {$tpf}nova_tags where tag_id!=$tag_id and tag_name='$tag_name';");
		$total_num = $rs['total_num'];
	    if($total_num>0){
	    	echo '{"code":"0","msg":"tag_name_exists"}';
			return;
	    }
	    
	    $db->query("update {$tpf}nova_tags set ".$db->sql_array($upds)." where tag_id=$tag_id ;");

		echo '{"code":"1","msg":"update_tag_ok"}';
		break;
		break;
	case 'deltag':
		$tag_id = (int)gpc('tag_id','G',0);
	    
	    $rs = $db->fetch_one_array("select count(*) as total_num from {$tpf}nova_tags where tag_id=$tag_id;");
		$total_num = $rs['total_num'];
	    if($total_num<=0){
	    	echo '{"code":"0","msg":"tag_not_exists"}';
			return;
	    }
	    
	    $db->query("delete from {$tpf}nova_tags where tag_id=$tag_id ;");

		echo '{"code":"1","msg":"delete_tag_ok"}';
		break;
		break;
}

include PHPDISK_ROOT."./includes/footer.inc.php";

?>